package com.comdo.newup_loan.module.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.bank.BankUtils;
import com.comdo.newup_loan.base.BaseFragment;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.comom.GlideCircleTransform;
import com.comdo.newup_loan.model.UserInfoBean;
import com.comdo.newup_loan.module.bankInfo.BankInfoActivity;
import com.comdo.newup_loan.module.flow.TradeFlowActivity;
import com.comdo.newup_loan.module.home.WantLoanActivity;
import com.comdo.newup_loan.module.login.LoginActivity;
import com.comdo.newup_loan.module.myinfo.MyInfoActivity;
import com.comdo.newup_loan.module.notice.MessageActivity;
import com.comdo.newup_loan.module.recharge.MineTopUpActivity;
import com.comdo.newup_loan.module.setting.SettingActivity;
import com.comdo.newup_loan.module.withdrawals.WithdrawalsActivity;
import com.comdo.newup_loan.util.CommonUtil;
import com.comdo.newup_loan.util.DialogUtil;
import com.comdo.newup_loan.util.StringUtils;
import com.liji.takephoto.TakePhoto;

import butterknife.BindView;

public class MineFragment extends BaseFragment<MinePresenter> implements MineContract.View, View.OnClickListener {
    @BindView(R.id.main_srl)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.img_user_pic)
    ImageView userPicImageView;
    @BindView(R.id.text_userinfo)
    TextView userInfoText;
    @BindView(R.id.text_wyj_title)
    TextView vyjTitleTextView;
    @BindView(R.id.text_wyj)
    TextView vyjTextView;
    @BindView(R.id.tv_available)
    TextView availableTextView;
    @BindView(R.id.tv_returnAmount)
    TextView returnAmountTextView;
    @BindView(R.id.text_recharge)
    TextView recharheTextView;
    @BindView(R.id.text_withdraw)
    TextView withDrawTextView;
    @BindView(R.id.tv_bankinfo)
    TextView bankInfoTextView;
    @BindView(R.id.tv_flow)
    TextView flowTextView;
    @BindView(R.id.tv_set)
    TextView setTextView;
    @BindView(R.id.img_loan)
    ImageView loanImageView;
    @BindView(R.id.btn_login)
    Button loginButton;
    @BindView(R.id.img_message)
    ImageView msgImageView;
    @BindView(R.id.rl_red)
    RelativeLayout mRedLayout;
    @BindView(R.id.tv_red)
    TextView mRedText;
    //页面是否可见
    private boolean isHidden;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void init() {
        isHidden = false;

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loginButton.setOnClickListener(this);

        userPicImageView.setOnClickListener(this);
        userInfoText.setOnClickListener(this);
        vyjTitleTextView.setOnClickListener(this);
        vyjTextView.setOnClickListener(this);
        recharheTextView.setOnClickListener(this);
        withDrawTextView.setOnClickListener(this);

        bankInfoTextView.setOnClickListener(this);
        flowTextView.setOnClickListener(this);
        setTextView.setOnClickListener(this);
        loanImageView.setOnClickListener(this);
        mRedLayout.setOnClickListener(this);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getUserInfo(Config.loginInfo.getUserId());
            }
        });
    }

    @Override
    public MinePresenter getmPresenter() {
        return new MinePresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //登录，登出
            case R.id.btn_login:
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                //builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("确认退出：");
                //    设置一个PositiveButton
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CommonUtil.exitLogin(getActivity());
                    }
                });
                builder.setNegativeButton("取消"
                        , new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.show();

                break;
            //用户头像
            case R.id.img_user_pic:
                TakePhoto takePhotoback = new TakePhoto(getActivity());
                takePhotoback.setOnPictureSelected(new TakePhoto.onPictureSelected() {
                    @Override
                    public void select(String path, String compresspath) {
                        mPresenter.upLoad(Config.loginInfo.getUserId(), compresspath);
                    }
                });
                takePhotoback.show();
                break;
            //用户信息
            case R.id.text_userinfo:
                if (!BankUtils.hasBankRegisterDialog()) {
                    toUserInfoActivity();
                } else {
                    DialogUtil.getToBankRegister(getActivity());
                }

                break;
            //交违约金
            case R.id.text_wyj_title:
                if (!BankUtils.hasBankRegisterDialog()) {
                    toPenaltyActivity();
                } else {
                    DialogUtil.getToBankRegister(getActivity());
                }

                break;
            //交违约金
            case R.id.text_wyj:
                if (!BankUtils.hasBankRegisterDialog()) {
                    toPenaltyActivity();
                } else {
                    DialogUtil.getToBankRegister(getActivity());
                }
                break;
            //充值
            case R.id.text_recharge:
                if (!BankUtils.hasBankRegisterDialog()) {
                    toRechargeActivity();
                } else {
                    DialogUtil.getToBankRegister(getActivity());
                }

                break;
            //提现
            case R.id.text_withdraw:
                if (!BankUtils.hasBankRegisterDialog()) {
                    toWithDrawalsActivity();
                } else {
                    DialogUtil.getToBankRegister(getActivity());
                }

                break;
            //银行信息
            case R.id.tv_bankinfo:
                toBankInfoActivity();
                break;
            //交易流水
            case R.id.tv_flow:
                if (!BankUtils.hasBankRegisterDialog()) {
                    toFlowActivity();
                } else {
                    DialogUtil.getToBankRegister(getActivity());
                }
                break;
            //设置
            case R.id.tv_set:
                toSetActivity();
                break;
            //我要借款
            case R.id.img_loan:
                toLoanActivity();
                break;
            //系统消息
            case R.id.rl_red:
                toMessageActivity();
                break;
        }
    }

    private void toLoginActivity() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }

    private void toBankInfoActivity() {
        Intent intent = new Intent(getActivity(), BankInfoActivity.class);
        startActivity(intent);
    }

    private void toFlowActivity() {
        Intent intent = new Intent(getActivity(), TradeFlowActivity.class);
        startActivity(intent);
    }

    private void toSetActivity() {
        Intent intent = new Intent(getActivity(), SettingActivity.class);
        startActivity(intent);
    }

    private void toUserInfoActivity() {
        Intent intent = new Intent(getActivity(), MyInfoActivity.class);
        startActivity(intent);
    }

    private void toPenaltyActivity() {
        Intent intent = new Intent(getActivity(), PenaltyActivity.class);
        startActivity(intent);
    }

    private void toMessageActivity() {
        Intent intent = new Intent(getActivity(), MessageActivity.class);
        startActivity(intent);
    }

    private void toRechargeActivity() {
        Intent intent = new Intent(getActivity(), MineTopUpActivity.class);
        startActivity(intent);
    }

    private void toWithDrawalsActivity() {
        Intent intent = new Intent(getActivity(), WithdrawalsActivity.class);
        startActivity(intent);
    }

    private void toLoanActivity() {
        Intent intent = new Intent(getActivity(), WantLoanActivity.class);
        startActivity(intent);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isHidden = hidden;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (Config.loginInfo == null) {
            toLoginActivity();
        } else {
            if (!isHidden)
                mPresenter.getUserInfo(Config.loginInfo.getUserId());
        }
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
        if (mRefreshLayout.isRefreshing())
            mRefreshLayout.setRefreshing(false);
    }

    @Override
    public void getUserInfo(UserInfoBean userInfo) {
        Config.userInfo = userInfo;
        if (mRefreshLayout.isRefreshing())
            mRefreshLayout.setRefreshing(false);
        userInfoText.setText(userInfo.getPhone());
        vyjTextView.setText(userInfo.getPenaltyAmount());
        returnAmountTextView.setText(userInfo.getShouldReturnAmount());
        if(!StringUtils.isNull(Config.userInfo.getAvailable()))
        availableTextView.setText(StringUtils.getMoneyString(Double.parseDouble(Config.userInfo
                .getAvailable())));
        if (userInfo.getTotalNotReadNum() > 0) {
            mRedText.setVisibility(View.VISIBLE);
            mRedText.setText(userInfo.getTotalNotReadNum() > 99 ? "99" : userInfo.getTotalNotReadNum() + "");
        } else {
            mRedText.setVisibility(View.GONE);
        }
        if (StringUtils.isNull(userInfo.getPortrait())) {
            Glide.with(getActivity()).load(R.drawable.user_pic)
                    .transform(new GlideCircleTransform(getActivity()))
                    .into(userPicImageView);
        } else {
            Glide.with(getActivity()).load(Config.PIC_FORE + userInfo.getPortrait())
                    .transform(new GlideCircleTransform(getActivity())).error(R.drawable.user_pic)
                    .into(userPicImageView);
        }
    }

    @Override
    public void upLoad(String result) {
        Glide.with(getActivity()).load(Config.PIC_FORE + result)
                .transform(new GlideCircleTransform(getActivity()))
                .into(userPicImageView);
    }
}
